package com.xyc.servlet.song;

import com.xyc.mapper.SongMapper;
import lombok.SneakyThrows;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/updateSong")
public class UpdateSongServlet extends HttpServlet {
    @SneakyThrows
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取页面上的内容加入map
        req.setCharacterEncoding("utf-8");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String id = req.getParameter("song_id");
        String name = req.getParameter("name");
        String author = req.getParameter("author");
        Date start = sdf.parse(req.getParameter("start"));
        int isVip = Integer.parseInt(req.getParameter("isVip"));
        String list = req.getParameter("list");
        String memo = req.getParameter("memo");

        Map map = new HashMap();
        //将需要更新的数据加到map
        map.put("Song_id", id);
        map.put("author", author);
        map.put("start", start);
        map.put("name", name);
        map.put("memo", memo);
        map.put("isVip", isVip);
        map.put("list", list);


        //System.out.println("显示 更新的内容:"+map);
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-dao.xml");
        SongMapper songMapper = ctx.getBean("songMapper", SongMapper.class);
        songMapper.updateSong(map); // 更新
        req.getRequestDispatcher("mySong").forward(req, resp);
    }
}
